#!/usr/bin/env node
// ///////////
// var argv = process.argv;
// // console.log('arguments number:',argv.length);
// // console.log('arguments:',argv);

// var expression = argv[2];
// console.log(expression + '=%d', eval(expression));
// 02-calc.js

function printUsage() {
    console.log('Usage: node 02-calc.js <expression>');
    console.log('Example: node 02-calc.js 2+2*2');
  }
  
  function calculateExpression(expr) {
    try {
      // 使用 Function 构造函数来计算表达式
      const result = new Function('return ' + expr)();
      console.log('Result:', result);
    } catch (error) {
      console.error('Error: Invalid mathematical expression');
    }
  }
  
  // 主逻辑
  const args = process.argv.slice(2);
  
  if (args.length === 0 || args.includes('--help') || args.includes('-h')) {
    printUsage();
  } else if (args.length > 3) {
    console.log('Warning: More than three arguments provided. Only the first three will be considered.');
    const expression = args.slice(0, 3).join(' ');
    calculateExpression(expression);
  } else {
    const expression = args.join(' ');
    calculateExpression(expression);
  }



